//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Conditional story branch node.
/// </summary>
[AddComponentMenu("ViNo/Nodes/Label_Jump/Condition")]
[ System.Serializable ]
public class ConditionNode : ViNode {
	public ViNoFlagType m_FlagType;
	public FlagTable m_FlagTable;
	public string m_FlagName;
	
	public ViNode m_IFTarget;
	public ViNode m_ELSETarget;
		
	public override void ToScenarioScript( ref System.Text.StringBuilder sb ){
		Hashtable attr = new Hashtable();
		attr[ "if" ] = "*" + m_IFTarget.GetNodeLabel();
		attr[ "else" ] = "*" + m_ELSETarget.GetNodeLabel();
		attr[ "exp" ] = "\"" + m_FlagName + "==" + m_FlagType.ToString().ToLower() + "\"";
		sb.Append( GetScript( "condition" , attr ) );
	}

	public override void ToByteCode( ByteCodes code ){
		List<byte> byteList = new List<byte>();
		
//		AddNodeCodeWithTag( byteList , name );
		AddNodeCode( byteList );
		
		if( m_IFTarget != null && m_ELSETarget != null ){						
			byteList.Add( Opcode.IF );
			ByteCodeScriptTools.AddStringCode( byteList , m_FlagName );			
			if( m_FlagType == ViNoFlagType.TRUE ){
//				ByteCodeScriptTools.AddStringCode( byteList , "true" );			
				byteList.Add( 1 );
			}
			else{
//				ByteCodeScriptTools.AddStringCode( byteList , "false" );							
				byteList.Add( 0 );
			}			
			string ifTag = m_IFTarget.GetNodeTag( m_IFTarget.name );			
			string elseTag = m_ELSETarget.GetNodeTag( m_ELSETarget.name );			
			
			ByteCodeScriptTools.AddStringCode( byteList , ifTag );
			ByteCodeScriptTools.AddStringCode( byteList , elseTag );

			code.Add( byteList.ToArray() );
		}
		else{			
			Debug.LogError( "JumpTargetNode:" + name + " isn't set IF target or ELSE target." );					
		}
		
		code.Add( Opcode.STOP );

		ToByteCodeInternal( code );		
	}
	
}
